package com.rivercloud.dao.Impl;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.rivercloud.dao.TableDao;
import com.rivercloud.entity.ExcelTable;
import com.rivercloud.entity.Template;

public class TableDaoImpl implements TableDao {

	static SqlSessionFactory sqlSessionFactory = null;

	static {
		SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}

		sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
	}

	@Override
	public void addTable(ExcelTable excelTable) {
		SqlSession session = sqlSessionFactory.openSession();
		session.insert("addTable", excelTable);
		
		session.commit();
		session.close();
	}

	@Override
	public int findIdByTableName(String tableName) {
		SqlSession session = sqlSessionFactory.openSession();

		ExcelTable table = session.selectOne("findIdByTableName", tableName);
		session.close();
		return table.getId();
	}

	@Override
	public List<ExcelTable> findAll() {
		try {
			SqlSession sqlSession = sqlSessionFactory.openSession();
			List<ExcelTable> tableList = sqlSession.selectList("findAllTable");
			
			return tableList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public ExcelTable checkTableName(String tableName) {
		SqlSession session = sqlSessionFactory.openSession();

		ExcelTable table = session.selectOne("checkTableName", tableName);
		
		session.close();
		return table;
	}

	@Override
	public ExcelTable findTableById(int tableId) {
		SqlSession session = sqlSessionFactory.openSession();

		ExcelTable table = session.selectOne("findTableById", tableId);
		session.close();
		return table;
	}

	@Override
	public List<ExcelTable> findTableByProjectId(int projectId) {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		List<ExcelTable> tableList = sqlSession.selectList("findTableByProjectId");
		
		return tableList;
	}

	@Override
	public List<ExcelTable> findTablesByProjectId(int projectId) {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		List<ExcelTable> tableList = sqlSession.selectList("findTablesByProjectId",projectId);
		
		return tableList;
	}

	@Override
	public void deleteTable(Integer tableId) {
		SqlSession session = sqlSessionFactory.openSession();
		session.delete("deleteTable", tableId);
		
		session.commit();
		session.close();
	}

	@Override
	public void updateFinish(int tableId) {
		SqlSession session = sqlSessionFactory.openSession();
		session.delete("updateFinish", tableId);
		
		session.commit();
		session.close();
	}

	@Override
	public List<ExcelTable> findFinishTable(ExcelTable table) {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		List<ExcelTable> tableList = sqlSession.selectList("findFinishTable",table);
		
		return tableList;
	}

	@Override
	public int findProjectIdByTableId(int tableId) {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		int projectId = sqlSession.selectOne("findProjectIdByTableId",tableId);
		
		return projectId;
	}

	@Override
	public int lastInsertId() {
		SqlSession session = sqlSessionFactory.openSession();

		int id = session.selectOne("lastInsertTableId");
		session.close();
		return id;
	}

	@Override
	public List<ExcelTable> findTableByTableIdList(List<Integer> tableIdList) {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		List<ExcelTable> tableList = sqlSession.selectList("findTableByTableIdList",tableIdList);
		
		return tableList;
	}

	@Override
	public List<ExcelTable> findTablesByUserIdAndProjectId(ExcelTable excelTable) {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		List<ExcelTable> tableList = sqlSession.selectList("findTablesByUserIdAndProjectId",excelTable);
		
		return tableList;
	}
	
	
	public static void main(String[] args) {
		ExcelTable table = new ExcelTable();
		table.setProjectId(48);
		table.setUserId(2);
		List<ExcelTable> list2 = new TableDaoImpl().findTablesByUserIdAndProjectId(table);
		for (ExcelTable excelTable : list2) {
			System.out.println(excelTable);
		}
	}

	

}
